package com.itlong.cloud.app;

import com.itlong.cloud.equip.app.IAppCloudElevatorCallStatusServiceClient;
import com.itlong.cloud.utils.date.DateUtil;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;

/**
 * <desc>
 *      用户预约呼梯状态维护timetask
 * </desc>
 *
 * @createDate 2018/04/13
 */
@EnableScheduling // 启用定时任务
@RestController
public class AppCloudElevatorCallStatusTimeTask {
    private static Logger LOG = LoggerFactory.getLogger(AppCloudElevatorCallStatusTimeTask.class);

    @Autowired
    IAppCloudElevatorCallStatusServiceClient iAppCloudElevatorCallStatusServiceClient;
    /**
     * <desc>
     *      删除一天前的用户呼梯状态
     * </desc>
     *
     * @author Jiaqi.X
     * @creatDate 2017/11/10
     */
    @Scheduled(cron = "0 0 2 * * ?") // 每天凌晨2点执行
    public void deleteYestodayCallStatus() {
        try {
            iAppCloudElevatorCallStatusServiceClient.deleteYestodayCallStatus();
//            LOG.info("【定时任务】删除一天前的用户呼梯状态执行成功，当前时间：" + DateUtil.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss"));
        }catch (Exception e){
            LOG.error("【定时任务】删除一天前的用户呼梯状态执行失败，当前时间：" + DateUtil.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss:"),e);
        }
    }


    /**
     * <desc>
     *      凌晨两点执行冷表转移
     * </desc>
     *
     * @author Jiaqi.X
     * @creatDate 2017/11/10
     */
    @Scheduled(cron = "0 3 2 * * ?") // 每天凌晨2点执行
    public void coldDataMove() {
        try {
            iAppCloudElevatorCallStatusServiceClient.coldDataRemove();
        }catch (Exception e){
            LOG.error("【定时任务】冷数据转移用户呼梯状态执行失败，当前时间：" + DateUtil.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss:"),e);
        }
    }

    /**
     * <desc>
     *      将所有超过1分钟都没有预约完成的电梯状态修改为呼梯失败
     * </desc>
     *
     * @return
     * @author Jiaqi.X
     * @createDate 2018/04/13
     */
    @Scheduled(cron = "0 0/1 * * * ?") // 每过1分钟执行一次
    public void setCallStatusAsCallDefeat(){
        try {
            iAppCloudElevatorCallStatusServiceClient.setCallStatusAsCallDefeat();
//            LOG.info("【定时任务】将所有超过10分钟都没有预约完成的电梯状态修改为呼梯失败执行成功，当前时间：" + DateUtil.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss"));
        }catch (Exception e){
            LOG.error("【定时任务】将所有超过10分钟都没有预约完成的电梯状态修改为呼梯失败执行失败，当前时间：" + DateUtil.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss:"),e);
        }
    }
}
